package com;

import com.mapper.IClassMapper;
import com.mapper.IStudentMapper;
import com.pojo.IClass;
import com.pojo.IStudent;
import com.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class StudentTest {
    @Test
    public void findStudentByTest(){
        //1.通过工具类获取SqlSession对象
        SqlSession session = MyBatisUtils.getSqlSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        //2.使用IStudentMapper对象查询id为1的学生的信息
        IStudent student = mapper.selectStudent(2);
        System.out.println(student.toString());
        //3.关闭SqlSession
        session.close();
    }
    @Test
    public void updateIStudentTest(){
        SqlSession session = MyBatisUtils.getSqlSession();
        IStudent student = new IStudent();
        student.setId(4);
        student.setName("李雷");
        student.setAge(21);
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        int result = mapper.updateStudent(student);
        if(result>0){
            System.out.println("更新成功"+result+"条数据");
        }
        else {
            System.out.println("更新数据失败");
        }
        System.out.println(student.toString());
        session.commit();
        //关闭SqlSession
        session.close();
    }
    @Test
    public void selectClassByIdTest() {
        // 1.通过工具类生成SqlSession对象
        SqlSession session = MyBatisUtils.getSqlSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        // 2.查询id为2的班级中学生的信息
        IClass icalss = mapper.selectClassById(2);
        System.out.println(icalss.toString());
        session.close();
    }
}